package com.kun.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kun.dao.ProvinceDao;
import com.kun.dao.impl.ProvinceDaoImpl;
import com.kun.domain.Province;
import com.kun.jedis.utils.JedisPoolUtils;
import com.kun.service.ProvinceService;
import redis.clients.jedis.Jedis;

import java.util.List;

public class ProvinceServiceImpl implements ProvinceService {

    //声明dao
    private ProvinceDao provinceDao= new ProvinceDaoImpl();

    @Override
    public List<Province> findAll() {
        return provinceDao.findAll();
    }

    /**
     * 使用redis缓存
     */
    @Override
    public String findAllJson() {
        //1.先从redis中查询数据
        //1.1 获取redis客户端连接
        Jedis jedis = JedisPoolUtils.getJedis();
        String province_json = jedis.get("province");
        //System.out.println("province_json : "+province_json);

        //2.判断province_json数据是否为null
        if(province_json == null || province_json.length() == 0){
            //redis中没有数据
            System.out.println("redis中没有数据，查询数据库");
            //2.1 从数据库中查询
            List<Province> list = provinceDao.findAll();
            //2.2 将list序列化为json
            ObjectMapper mapper = new ObjectMapper();
            try {
                province_json = mapper.writeValueAsString(list);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            //2.3 将json数据存入到redis中
            jedis.set("province",province_json);
            //归还连接
            jedis.close();
        }else{
            System.out.println("redis中有数据，查询缓存。。。");
        }
        return province_json;
    }
}
